class Solution {
    public String reverseOnlyLetters(String s) {
        int l=0; 
        int r = s.length()-1;
        while(l<r){
            while(l<r&&!((s.charAt(l)-'a'>=0&&s.charAt(l)-'z'<=0)||(s.charAt(l)-'A'>=0&&s.charAt(l)-'Z'<=0))) l++;
            while(l<r&&!((s.charAt(r)-'a'>=0&&s.charAt(r)-'z'<=0)||(s.charAt(r)-'A'>=0&&s.charAt(r)-'Z'<=0))) r--;
            if(l==r)break;
            s = s.substring(0, l)+s.charAt(r)+s.substring(l+1, r)+s.charAt(l)+s.substring(r+1);
            l++;
            r--;
        }
        return s;
    }
}